const Koa = require('koa');
const Moment = require("moment");
const KoaLogger = require("koa-logger");
const Router = require('koa-router');
const cors = require('koa2-cors');
const bodyParser = require('koa-bodyparser');

const URL_PREFIX='/koa'
const router = new Router({ prefix: URL_PREFIX });

const port = 3002
const app = new Koa();
const logger = KoaLogger((str) => { // 使用日志中间件
  console.log(Moment().format('YYYY-MM-DD HH:mm:ss')+str);
});

app.use(cors());
app.use(bodyParser());
require('./config/eureka-helper').registerWithEureka('koa', port)


router.get('/getTest', (ctx) => {
  ctx.body = {
    name: 'test',
  };
});

router.post('/login', (ctx) => {
  ctx.body = {
    code: 0,
    success: true,
    result: {
      userId: '1',
      username: 'vben',
      realName: 'Vben Admin',
      desc: 'manager',
      password: '123456',
      token: 'fakeToken1',
      roles: [
        {
          roleName: 'Super Admin',
          value: 'super',
        },
      ],
    },
  };
});

router.get('/getUserInfoById', (ctx) => {
  ctx.body = {
    code: 0,
    success: true,
    result: {
      userId: '1',
      username: 'vben',
      realName: 'Vben Admin',
      desc: 'manager',
      password: '123456',
      token: 'fakeToken1',
      roles: [
        {
          roleName: 'Super Admin',
          value: 'super',
        },
      ],
    },
  };
});

app.use(logger);
app.use(router.routes()).use(router.allowedMethods());

app.listen(port, () => {
  console.log('server is listen in 3002');
});
